<?php require_once('Connections/CSRE_Connection.php'); ?>
<?php include('CSRE_login_control.php') ?>

<?php 

	$total_tax = 0;
	$total_non_tax = 0;
	$max_fs = 0;		
	
	if (isset($_SESSION["products"])) {

		$keys = array_keys($_SESSION["products"]);

		foreach ($keys as $key) {

			mysql_select_db($database_CSRE_Connection, $CSRE_Connection);
			$query_productById = sprintf("SELECT * FROM csre_product WHERE product_id = %s", $key);
			$productById = mysql_query($query_productById, $CSRE_Connection) or die(mysql_error());
			$row_productById = mysql_fetch_assoc($productById);
			$totalRows_productById = mysql_num_rows($productById);

			$value = $_SESSION["products"][$key]['units'] * $row_productById['price'];

			if ($row_productById['taxable'] == 1) {
				$total_tax = $total_tax + $value;
			} else {
				$total_non_tax = $total_non_tax + $value;
			}

			if ($_SESSION["products"][$key]["service"] > $max_fs) {
				$max_fs = $_SESSION["products"][$key]["service"];
			}
			
		}
	}
	

?>

<br />

<table class="text_form" cellspacing="0" cellpadding="3" 
	style="border: #5995CA thin solid; font-family:Verdana, Arial, Helvetica, sans-serif;" >
	<tr>
		<td width="160" style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">Non Tax Produit</td>
		<td align="right" width="100">
			$&nbsp;<?php echo number_format($total_non_tax,2,',','.'); ?>
		</td>
	</tr>
	<tr>
		<td width="130" style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">Tax Produit</td>
		<td align="right" width="100">
			$&nbsp;<?php echo number_format($total_tax,2,',','.'); ?>
		</td>
	</tr>
	<tr>
		<td style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">Frais de service</td>
		<td align="right">
		
			<?php 
			
				$total = $total_tax + $total_non_tax;
			
				mysql_select_db($database_CSRE_Connection, $CSRE_Connection);
				$query_Recordset1 = sprintf("select value from csre_frais_service fs where fs.minim <= %s and fs.maxim >= %s 
				and fs.service_code = %s ", $total, $total, $max_fs);
				
				//echo $query_Recordset1;
				
				$Recordset1 = mysql_query($query_Recordset1, $CSRE_Connection) or die(mysql_error());
				$row_Recordset1 = mysql_fetch_assoc($Recordset1);

				$fraisService = $row_Recordset1['value'];
			
				if (($total_tax + $total_non_tax) >= 251) {
					$frais = $total * $fraisService / 100;  
				} else {
					$frais = $fraisService; 
				}
			
			?>
		
		
			$&nbsp;<?php echo number_format($frais,2,',','.'); ?>
		</td>
	</tr>
	<tr>
		<td style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">TPS</td>
		<td align="right">
			$&nbsp;<?php $tps = (($total_tax + $frais) * 0.05); echo number_format($tps ,2,',','.'); ?>
		</td>
	</tr>
	<tr>
		<td style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">TVQ</td>
		<td align="right">
			$&nbsp;<?php $tvq = ($total_tax + $frais + $tps) * 0.085; echo number_format($tvq ,2,',','.'); ?>
		</td>
	</tr>
	<tr>
		<td style="background-color: #8EA5D9; color:#FFFFFF; font-weight:bold;">Total</td>
		<td align="right">
			$&nbsp;<?php echo number_format($tvq + $tps +  $frais + $total,2,',','.'); ?>
		</td>
	</tr>
</table>